<template>
  <div style="background-color: #eee;height: 100%">
    <div class="mapContent">
      <div class="top">
        <div class="content">
          <div :class="activeWareIndex == index ? 'item active' : 'item'" v-for="(warehouse, index) in warehouseList"
            :key="warehouse.id" @click="changeWarehouse(index)">
            <div class="tit">
              <img src="@/assets/img/house_icon.png" alt="">
              <h3>{{ warehouse.name }}</h3>
            </div>
            <p class="address">{{ warehouse.code + " (" + warehouse.address + ")" }}</p>
            <div class="tag">
              <span>空闲：{{ warehouse.totalPosition - warehouse.usedPosition }}</span>
              <span>占用：{{ warehouse.usedPosition }}</span>
              <span>总量：{{ warehouse.totalPosition }}</span>
            </div>
            <div class="progress">
              <span>{{ warehouse.usedPosition }} / {{ warehouse.totalPosition }}(个)</span>
              <div class="pro">
                <p
                  :style="{ 'width': warehouse.totalPosition == 0 ? '0%' : Math.floor((warehouse.usedPosition / warehouse.totalPosition) * 100) + '%' }">
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="center">
        <div class="inner">
          <div class="content">
            <span>区域：</span>
            <p :class="activeRegionIndex == index ? 'active' : ''" v-for="(region, index) in regionList" :key="region.id"
              @click="changeRegion(index)">{{ region.name }}</p>
          </div>
        </div>
        <div class="inner">
          <div class="content">
            <span>通道：</span>
            <p :class="activePosIndex == index ? 'active' : ''" v-for="(aisle, index) in aisleList" :key="aisle.id"
              @click="changeAisle(index)">{{ aisle.name }}</p>
          </div>
        </div>
      </div>
      <div class="bottom">
        <el-row>
          <el-form :model="searchForm" ref="form" label-width="80px" :inline="false" size="small"
            :disabled="warehouseList.length === 0 && regionList.length === 0 && aisleList.length === 0">
            <el-col :span="4">
              <el-form-item label="仓位状态">
                <el-select v-model="searchForm.status" style="width: 100%;" @change="getPosition">
                  <el-option label="全部" value="0"></el-option>
                  <el-option label="已占用" value="1"></el-option>
                  <el-option label="未占用" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="库位编号">
                <el-input v-clearEmoji placeholder="请输入库位编号" clearable suffix-icon="el-icon-search"
                  v-model.trim="searchForm.value" @keyup.native.enter="getPosition" @clear="getPosition">
                </el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <div class="position flex mb10 ">
          <p :class="pos.status == 1 ? 'bg-red' : 'bg-green'" v-for="pos in positionList" :key="pos.id"
            @mouseover="e => showPop(e, pos)" @mouseout="delPop">{{ pos.code }}</p>
        </div>
        <el-popover class="inner" placement="bottom-start" width="200" trigger="hover" ref="pop1" v-model="visible">
          <div>
            <div class="popover">
              <span class="tit">商品名称</span>
              <span>{{ posObj.commodityName }}</span>
              <span class="tit">数量</span>
              <span>{{ posObj.inventoryNum }}个</span>
            </div>
          </div>
        </el-popover>
      </div>

      <div class="fixed flex flex-i-c">
        <p class="bg-red m0 mr10 tip"></p>
        <h3 class="fn m0 mr10 f14">已使用 {{ hasUsed }}</h3>
        <p class="bg-green m0 mr10 tip"></p>
        <h3 class="fn m0 mr10 f14">未使用 {{ noUsed }}</h3>
      </div>
    </div>
  </div>
</template>

<script>
import { reqWarehouseInfo, reqRegionListByWarehouseId, reqPositionInfo } from "@/api/storage";
import { reqAisleListById } from "@/api/warehouse"
export default {
  name: "PositionMap",
  data() {
    return {
      searchForm: {
        status: "0",
        value: "",
      },
      visible: false,
      activeWareIndex: 0,
      activeRegionIndex: 0,
      activePosIndex: 0,
      warehouseList: [],
      regionList: [],
      aisleList: [],
      positionList: [],

      warehouseId: "",
      regionId: "",
      aisleId: "",
      posObj: {},
      hasUsed: 0,
      noUsed: 0,
    }
  },
  watch: {
    warehouseId(n, o) {
      this.regionId = ''
      this.aisleId = ''
      this.hasUsed = 0
      this.noUsed = 0
      this.regionList = [];
      this.aisleList = [];
      this.positionList = [];
      this.searchForm.status = "0";
      this.getRegionList();
    },
    regionId(n, o) {
      this.aisleId = ''
      this.hasUsed = 0
      this.noUsed = 0
      this.aisleList = [];
      this.positionList = [];
      this.searchForm.status = "0";
      this.getAisleList();
    },
    aisleId(n, o) {
      this.hasUsed = 0
      this.noUsed = 0
      this.positionList = [];
      this.searchForm.status = "0";
      this.getPosition();
    },
  },
  mounted() {
    this.getWarehouseList();
  },
  methods: {
    showPop(e, obj) {
      this.posObj = obj
      if (this.posObj.status == "2") {
        this.posObj.commodityName = "无";
        this.posObj.inventoryNum = 0;
      }
      //关键代码
      //先隐藏并销毁之前显示的
      this.visible = false
      var pop1 = this.$refs.pop1
      pop1.doDestroy(true)
      this.$nextTick(() => {
        //显示新的
        pop1.referenceElm = pop1.$refs.reference = e.target
        this.visible = true
      })
    },
    delPop() {
      this.visible = false
    },
    async getWarehouseList() {
      let res = await reqWarehouseInfo();
      if (res.code === 200) {
        this.warehouseList = res.data;
        this.warehouseId = res.data[0]?.id;
        this.activeWareIndex = 0;
      }
    },
    changeWarehouse(index) {
      this.activeWareIndex = index;
      this.warehouseId = this.warehouseList[index]?.id;
    },
    async getRegionList() {
      let res = await reqRegionListByWarehouseId({ warehouseId: this.warehouseId });
      if (res.code === 200) {
        this.regionList = res.data;
        this.regionId = res.data[0]?.id;
        this.activeRegionIndex = 0;
      }
    },
    changeRegion(index) {
      this.activeRegionIndex = index;
      this.regionId = this.regionList[index]?.id;
    },
    async getAisleList() {
      if (!this.regionId) return;
      let res = await reqAisleListById({ regionId: this.regionId });
      if (res.code === 200) {
        this.aisleList = res.data;
        this.aisleId = res.data[0]?.id;
        this.activePosIndex = 0;
      }
    },
    changeAisle(index) {
      this.activePosIndex = index;
      this.aisleId = this.aisleList[index]?.id;
    },
    async getPosition() {
      this.hasUsed = 0
      this.noUsed = 0
      let { status, value } = this.searchForm;
      if (!this.aisleId) return
      let res = await reqPositionInfo({ aisleId: this.aisleId, status, value });
      if (res.code === 200) {
        this.positionList = res.data;
        this.hasUsed = 0;
        this.noUsed = 0;
        let noUsed = 0;
        let hasUsed = 0;
        this.positionList.forEach(element => {
          if (element.status == '2') {
            noUsed++;
          } else {
            hasUsed++;
          }
        });
        this.hasUsed = hasUsed;
        this.noUsed = noUsed;
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.mapContent {
  height: calc(100% - 46px);
  background-color: #eee;
  display: flex;
  flex-direction: column;

  .top,
  .center,
  .bottom {
    background-color: #fff;
    padding: 16px 24px;
    border-radius: 6px;

    .title {
      font-size: 16px;
      color: #000;
      font-weight: bold;
      margin-bottom: 10px;
    }
  }

  .top {
    margin-bottom: 16px;

    .content {
      display: flex;
      width: 100%;
      overflow-x: auto;

      .item {
        padding: 16px;
        border: 1px solid #dcdcdc;
        border-radius: 6px;
        margin-right: 24px;
        cursor: pointer;

        .tit {
          display: flex;
          align-items: center;

          img {
            width: 24px;
            height: 24px;
            margin-right: 8px
          }

          h3 {
            margin: 0;
            font-size: 20px;
            color: #333333;
          }
        }

        .address {
          padding-left: 32px;
          width: 300px;
          margin-top: 8px;
          margin-bottom: 12px;
          display: -webkit-box;
          /* 必须结合的属性 ，将对象作为弹性伸缩盒子模型显示 。*/
          -webkit-box-orient: vertical;
          /* 必须结合的属性 ，设置或检索伸缩盒对象的子元素的排列方式 。*/
          text-overflow: ellipsis;
          /* 可以用来多行文本的情况下，用省略号“…”隐藏超出范围的文本 。*/
          -webkit-line-clamp: 2;
          overflow: hidden;
          min-height: 35px;
        }

        .tag {
          display: flex;
          justify-content: space-between;

          span {
            width: 74px;
            height: 24px;
            font-size: 12px;
            line-height: 24px;
            text-align: center;
            background-color: #E7E7E7;

            &:nth-child(1) {
              color: #fff;
              background-color: #2BA471;
            }
          }
        }

        .progress {
          display: flex;
          flex-direction: column;

          span {
            width: 100%;
            text-align: right;
            margin-top: 12px;
            color: #cccccc;
            font-size: 12px;
          }

          .pro {
            background-color: #D9E1FF;
            border-radius: 6px;
            width: 100%;
            height: 12px;

            p {
              width: 80%;
              background-color: #0052D9;
              border-radius: 6px;
              margin: 0;
              height: 100%;
            }
          }
        }

        &.active {
          border-color: #0052D9;
        }
      }
    }
  }

  .center {
    margin-bottom: 16px;

    .inner {
      margin-bottom: 10px;

      p {
        margin: 0;
      }

      .tit {
        font-size: 12px;
        margin: 8px 0;
      }

      .content {
        display: flex;
        align-items: center;

        p {
          font-size: 14px;
          background-color: #E7E7E7;
          padding: 8px 12px;
          border-radius: 100px;
          max-width: 200px;
          text-align: center;
          margin-right: 10px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          cursor: pointer;

          &.active {
            color: #0052D9;
            background-color: #F2F3FF;
          }
        }
      }
    }
  }

  .bottom {
    flex: 1;

    .position {
      overflow: auto;
      flex-wrap: wrap;
      height: 284px;
      align-content: flex-start;
      color: #fff;

      &::-webkit-scrollbar {
        display: block;
        height: 8px;
        width: 8px;
      }

      /* 自定义滚动条的轨道 */
      &::-webkit-scrollbar-track {
        background: #f1f1f1;
      }

      /* 自定义滚动条的滑块 */
      &::-webkit-scrollbar-thumb {
        background: #a0a0a0;
      }

      /* 鼠标悬停在滑块上的样式 */
      &::-webkit-scrollbar-thumb:hover {
        background: #8b8b8b;
      }

      .inner {
        width: calc((100% - 150px) / 7);
        margin: 0 10px;
      }

      p {
        margin: 0;
        padding: 0;
        border-radius: 3px;
        width: calc((100% - 150px) / 7);
        // width: 100%;
        margin: 0 10px;
        cursor: pointer;
        height: 35px;
        text-align: center;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        line-height: 35px;
        margin-bottom: 25px;
      }
    }
  }

  .fixed {
    position: fixed;
    background-color: #fff;
    width: 100%;
    // left:-24px;
    margin-left: -24px;
    padding: 12px 0 12px 48px;
    bottom: 0;

    p {
      width: 22px;
      height: 22px;
      border-radius: 3px;
    }

    h3 {
      margin-right: 32px;
    }
  }
}

.popover {
  display: flex;
  flex-direction: column;

  .tit {
    color: #999999;
    font-size: 14px;
  }

  span {
    margin: 3px 0;
  }
}
</style>